package com.oceansoft.mobile.econsole.modules.api.controller;

import com.oceansoft.mobile.econsole.modules.base.entity.ApiPager;
import com.oceansoft.mobile.econsole.modules.base.entity.Query;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.weixin.entity.LostPerson;
import com.oceansoft.mobile.econsole.modules.weixin.service.ILostPersonService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * ClassName: ApiLostPersonController
 * Desc:  寻人寻亲
 * Date： 2017/3/29
 * Created：shaom
 */
@Controller
@RequestMapping("api/lostPerson")
public class ApiLostPersonController {

    @Resource
    private ILostPersonService lostPersonService;

    @RequestMapping("page")
    public String page(Query query, ModelMap model) {
        model.addAttribute("pager", lostPersonService.loadByPage(query));
        return "weixin/lostperson/lost_found";
    }


    @ResponseBody
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public Result loadLostPropertyByPage(Query query) {
        Result result = new Result();
        ApiPager<LostPerson> returnPage = lostPersonService.loadByPage(query);
        result.setSucc(Result.SUCC);
        result.setData(returnPage);
        result.setMsg("");
        result.setHasNext(returnPage.isHasNext());
        return result;
    }



    @RequestMapping(value = "details/{guid}", method = RequestMethod.GET)
    public String gotoDetails(@PathVariable("guid") String guid, Model model) {
        Result result = lostPersonService.findLostPersonById(guid);
        if (result.isSucc()) {
            LostPerson lostPerson = (LostPerson) result.getData();
            model.addAttribute("lostPerson", lostPerson);
            return "weixin/lostperson/lost_details";
        }
        model.addAttribute("lostProperty", null);
        return "weixin/lostperson/lost_details";
    }
}
